/**
 * 初始化禁忌症条目详情对话框
 */
var Obj = {

    contraindicationItemId: "",
    contraindicationItemRuleId: "",
    itemRuleCompare: "yes",

};


/**
 * 关闭此对话框
 */
Obj.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name)); //先得到当前iframe层的索引,再执行关闭
}

$(function () {
    Obj.getItemRule();
    //有无匹配规则
    var matchRule = Common_GetValue('matchRuleSwi') == true ? 1 : 0
    if(matchRule==false){
        $('#rulediv').attr("style","display:none;");
        $('#ruleExp').attr("style","visibility:hidden;");
        $('#ruleExp1').attr("style","visibility:hidden;");
    }else{
        $('#rulediv').attr("style","display:block;");
        $('#ruleExp').attr("style","visibility:visible;");
        $('#ruleExp1').attr("style","visibility:visible;");
    }
});

/**
 * 禁忌症条目匹配规则管理初始化
 */
Obj.ContraindicationItemRuleTable = $HUI.datagrid("#ContraindicationItemRuleTable", {
    fit: true,
    border: false,

    pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
    displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
    singleSelect: true,
    checkOnSelect: true,
    //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
    autoRowHeight: false,
    striped: true,
    rownumbers: true,
    loadMsg: '数据加载中...',
    pageSize: 10,
    pageList: [10,30,50],
    idField: "id",

    columns: [[

        {title: '规则代码', field: 'ruleCode', width: '100', align: 'left',  sortable: true},
        {title: '规则描述', field: 'ruleDesc', width: '280', align: 'left',  sortable: true},
        {title: '备注', field: 'comment', width: '260', align: 'left', valign: 'middle', sortable: true},
        {title: '规则类型', field: 'ruleType', width: '110', align: 'left', valign: 'middle', sortable: false},
        {field: 'calType', title: '计算类型', width: '200', align: 'left', sortable: false},
        {field: 'isDefault', title: '默认值', width: '110', align: 'left', sortable: false,formatter:function (value, row, index){
                if (value == 1) {
                    return "<span class='label label-success'>有</span>";
                } else {
                    return "<span class='label label-warning'>无</span>";
                }}
        },
    ]],
    onClickRow: function (rowIndex, rowData) {
        Obj.contraindicationItemRuleId = rowData.id;

    },
    onLoadSuccess: function (rowData) {

    }
});


Obj.compareAllRule = function () {

    Obj.itemRuleCompare = "all";
    Obj.getItemRule();
}


Obj.compareRelRule = function () {
    // var contraindicationItemId = Common_GetValue('contraindicationItemId');
    // if (contraindicationItemId == "" || contraindicationItemId == null) {
    //     Sys.info("请选择具体条目");
    //     return false;
    // }
    Obj.itemRuleCompare = "yes";
    Obj.getItemRule();
}

Obj.getItemRule = function () {
    var ajax = new $ax(Sys.backendPath + "/sdc/contraindicationitemrule/selectRuleByExpression", function (data) {
        $('#ContraindicationItemRuleTable').datagrid({loadFilter: pagerFilter}).datagrid('loadData', data);
    }, function (data) {
        layer.msg("查询失败!" + data.responseJSON.message + "!", {icon: 2});
    });

    ajax.set({"displayRule": Common_GetValue('expression')});
    ajax.set({"compare": Obj.itemRuleCompare});
    ajax.start();
}

Obj.addRule = function () {
    var href = Sys.backendPath + '/sds/sdsstandrulebase/to_add';
    var index = parent.layer.open({
        type: 2,
        title: ['<i class = "fa fa-object-group"></i>&nbsp;新增规则字典', 'padding-left:10px'],
        area: ['800px', '600px'], //宽高
        fix: false, //不固定
        skin: 'rbLBStyle  layer-ext-hisui',  //LB为layerBorder缩写
        //maxmin: true,
        content: href
    });
}

Obj.editRule = function () {

    if ( Obj.contraindicationItemRuleId == "" || Obj.contraindicationItemRuleId == null) {
        Sys.info("请选择具体规则");
        return false;
    }

    var href = Sys.backendPath + '/sds/sdsstandrulebase/to_update/' + Obj.contraindicationItemRuleId;
    var index = parent.layer.open({
        type: 2,
        title: ['<i class = "fa fa-object-group"></i>&nbsp;规则字典详情', 'padding-left:10px'],
        area: ['800px', '600px'], //宽高
        fix: false, //不固定
        skin: 'rbLBStyle  layer-ext-hisui',  //LB为layerBorder缩写
        content: href
    });
}

Obj.deleteRule = function () {

    if (Obj.contraindicationItemRuleId == "" || Obj.contraindicationItemRuleId == null) {
        Sys.info("请重新选择具体规则");
        return false;
    }
    var operation = function () {
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrulebase/delete", function (data) {
            Sys.success("删除成功!");
            Obj.getItemRule();
        }, function (data) {
            Sys.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("SdsRuleBaseId", Obj.contraindicationItemRuleId);
        ajax.start();
    };
    Sys.confirm("是否删除该规则字典?", operation);
    // var contraindicationItemId = Common_GetValue('contraindicationItemId');
    //
    // if (contraindicationItemId == "" || contraindicationItemId == null) {
    //     Sys.info("请选择禁忌症条目");
    //     return false;
    // }
    // if (Obj.contraindicationItemRuleId == "" || Obj.contraindicationItemRuleId == null) {
    //     Sys.info("请选择具体规则");
    //     return false;
    // }
    // var operation = function () {
    //     var ajax = new $ax(Sys.backendPath + "/sdc/contraindicationitemrule/deleteRule", function (data) {
    //         Sys.success("删除成功!");
    //         Obj.getItemRule();
    //     }, function (data) {
    //         Sys.error("删除失败!" + data.responseJSON.message + "!");
    //     });
    //     ajax.set("contraindicationItemRuleId", Obj.contraindicationItemRuleId);
    //     ajax.start();
    // };
    // Sys.confirm("是否删除该条目规则?", operation);
}



//保存条目信息
Obj.addSubmit = function () {
    var contraindicationItemId = Common_GetValue('contraindicationItemId');
    var contraindicationId=Common_GetValue('contraindicationId')
    var paramData = {};
    if (contraindicationItemId == "" || contraindicationItemId == null) {
        //insert
        paramData['contraindicationId']=contraindicationId;
    } else {
        //update
        paramData['id'] = contraindicationItemId;
    }

    //条目名称
    var itemName = Common_GetValue('itemName')
    //是否有效
    var active = Common_GetValue('active') == true ? 1 : 0
    //规则表达式
    var expression = Common_GetValue('expression')

    if (itemName == "" || itemName == null) {
        Sys.info("条目名称不能为空");
        return false;
    }

    paramData['itemName'] = itemName;
    paramData['active'] = active;
    paramData['expression'] = expression;


    console.info(paramData)
    //提交信息
    var ajax = new $ax(Sys.backendPath + "/sdc/contraindicationitem/save", function (res) {
        Sys.success(res.message);
        Obj.close();
        window.parent.contraindication.change();
    }, function (data) {
        Sys.error("保存失败!" + data.message + "!");
    });
    ajax.setData(paramData);
    ajax.start();

}

function pagerFilter(data) {
    if (typeof data.length == 'number' && typeof data.splice == 'function') {// is array
        data = {
            total: data.length,
            rows: data
        }
    }
    var dg = $(this);
    var opts = dg.datagrid('options');
    var pager = dg.datagrid('getPager');
    pager.pagination({
        onSelectPage: function (pageNum, pageSize) {
            opts.pageNumber = pageNum;
            opts.pageSize = pageSize;
            pager.pagination('refresh', {pageNumber: pageNum, pageSize: pageSize});
            dg.datagrid('loadData', data);
        }
    });
    if (!data.originalRows) {
        data.originalRows = (data.rows);
    }
    var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
    var end = start + parseInt(opts.pageSize);
    data.rows = (data.originalRows.slice(start, end));
    return data;
}

function Common_GetValue() {
    var itmValue = '';

    if (typeof (arguments[0]) !== 'string') return '';
    if (arguments[0] == '') return '';

    var $this = $('#' + arguments[0]);
    if ($this.length < 1) return '';

    var className = $this.attr("class").split(' ')[0];

    if (className == 'textbox') {  //文本框
        itmValue = $this.val();
    } else if (className == 'hisui-numberbox') {  //数字
        itmValue = $this.val();
    } else if (className == 'hisui-datebox') {  //日期
        itmValue = $this.datebox('getValue');
    } else if (className == 'hisui-timespinner') {  //时间框
        itmValue = $this.timespinner('getValue');
    } else if (className == 'hisui-combobox') {  //下拉框（多选下拉框没有封装）
        itmValue = $this.combobox('getValue');
    } else if (className == 'hisui-switchbox') {  //开关
        itmValue = $this.switchbox('getValue');
    } else if (className == 'hisui-checkbox') {  // 单个复选框
        itmValue = $this.checkbox('getValue');
    } else if (className == 'hisui-radio') {  //单个单选框
        itmValue = $this.radio('getValue');
    } else if (className == 'hisui-searchbox') {  //查询框框
        itmValue = $this.searchbox('getValue');
    }

    return itmValue;
}
